import echarts from 'echarts'
import testApi from '@/api/testApi'

export default {
  name: 'illegalPublicity',
  data() {
    return {
      illegalInfoList: [],
      chartsDatas: []
    }
  },
  created() {
  },

  async mounted() {
    await this.getList()
    this.makeCharts()
  },
  methods: {
    async getList() {
      await testApi.getIllegalInfoList().then((res) => {
        this.illegalInfoList = res.data
      }).catch((error) => {
        this.illegalInfoList = [
          {
            id: '0',
            supplierName: '阿里云',
            credenceValue: '90',
            punishmentLevel: '1',
            time: '2023-07-02',
            remark: ''
          },
          {
            id: '1',
            supplierName: '联通云',
            credenceValue: '92',
            punishmentLevel: '2',
            time: '2023-07-02',
            remark: ''
          },
          {
            id: '2',
            supplierName: '华为云',
            credenceValue: '95',
            punishmentLevel: '1',
            time: '2023-07-02',
            remark: ''
          },
          {
            id: '3',
            supplierName: '火山云',
            credenceValue: '90',
            punishmentLevel: '3',
            time: '2023-07-02',
            remark: ''
          },
          {
            id: '4',
            supplierName: '阿里云',
            credenceValue: '91',
            punishmentLevel: '4',
            time: '2023-07-02',
            remark: ''
          },
          {
            id: '5',
            supplierName: '华为云',
            credenceValue: '99',
            punishmentLevel: '2',
            time: '2023-07-02',
            remark: ''
          },
          {
            id: '6',
            supplierName: '火山云',
            credenceValue: '90',
            punishmentLevel: '3',
            time: '2023-07-02',
            remark: ''
          }
        ]
      })

      await testApi.getIllegalPublicityChartData().then((res ) => {
        this.chartsDatas = res.data
      }).catch((error) => {
        this.chartsDatas = [
          { id: 0, supplierName: '阿里云', punishmentNum: 55, level1Num: 22, level2Num: 13, level3Num: 18, level4Num: 2 },
          { id: 1, supplierName: '联通云', punishmentNum: 61, level1Num: 12, level2Num: 19, level3Num: 22, level4Num: 8 },
          { id: 2, supplierName: '华为云', punishmentNum: 71, level1Num: 13, level2Num: 17, level3Num: 33, level4Num: 5 },
          { id: 3, supplierName: '火山云', punishmentNum: 68, level1Num: 28, level2Num: 13, level3Num: 18, level4Num: 9 }
        ]
      })
    },
    makeCharts() {
      this.makePieChart()
      this.makeBarCharts()
    },
    fullScreen() {
      const element = document.getElementById('entirety')
      if (element.requestFullscreen) {
        element.requestFullscreen()
      } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen()
      } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen()
      } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen()
      }
    },
    getLevelInfo(level) {
      switch (level) {
        case '1':
          return {
            levelLabel: '一级',
            measureLabel: '警告',
            type: ''
          }
        case '2':
          return {
            levelLabel: '二级',
            measureLabel: '罚款',
            type: 'success'
          }
        case '3':
          return {
            levelLabel: '三级',
            measureLabel: '暂停服务',
            type: 'warning'
          }
        case '4':
          return {
            levelLabel: '四级',
            measureLabel: '重新审查',
            type: 'danger'
          }
      }
    },
    makePieChart() {
      const pieChart = echarts.init(document.getElementById('pieChart'), 'light')
      const data = []
      for (let i = 0; i < this.chartsDatas.length; i++) {
        data.push({
          value: this.chartsDatas[i].punishmentNum,
          name: this.chartsDatas[i].supplierName
        })
      }
      const option = {
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'horizontal',
          left: 'left',
          textStyle: {
            color: '#FFFFFF'
          }
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: '50%',
            data: data,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            label: {
              formatter: '{b|{b}：}\n{c}  {per|{d}%}  ',
              rich: {
                b: {
                  fontSize: 14,
                  fontWeight: 'bold',
                  lineHeight: 33
                },
                per: {
                  color: '#fff',
                  backgroundColor: '#4C5058',
                  padding: [3, 4],
                  borderRadius: 4
                }
              }
            }
          }
        ]
      }
      pieChart.setOption(option)
    },
    makeBarCharts() {
      const level1Chart = echarts.init(document.getElementById('level1Chart'), 'light')
      const level2Chart = echarts.init(document.getElementById('level2Chart'), 'light')
      const level3Chart = echarts.init(document.getElementById('level3Chart'), 'light')
      const level4Chart = echarts.init(document.getElementById('level4Chart'), 'light')

      const data = this.chartsDatas
      const level1Option = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'value',
          show: false,
          max: 'dataMax'
        },
        yAxis: {
          type: 'category',
          data: data.map(s => s.supplierName).reverse(),
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            },
            show: true
          },
          // 去掉刻度线
          axisLine: { 'show': false },
          axisTick: { 'show': false }
        },
        series: [{
          data: data.map(s => s.level1Num).reverse(),
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            normal: {
              label: {
                show: true, // 开启显示
                position: 'right',
                textStyle: { // 数值样式
                  color: '#5FA4E9',
                  fontSize: 12
                }
              },
              color: '#307CE0'
            }
          }
        }],
        grid: {
          containLabel: true,
          top: 5,
          left: 10,
          bottom: 10
        }
      }
      const level2Option = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'value',
          show: false,
          max: 'dataMax'
        },
        yAxis: {
          type: 'category',
          data: data.map(s => s.supplierName).reverse(),
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            },
            show: true
          },
          // 去掉刻度线
          axisLine: { 'show': false },
          axisTick: { 'show': false }
        },
        series: [{
          data: data.map(s => s.level2Num).reverse(),
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            normal: {
              label: {
                show: true, // 开启显示
                position: 'right',
                textStyle: { // 数值样式
                  color: '#5FA4E9',
                  fontSize: 12
                }
              },
              color: '#307CE0'
            }
          }
        }],
        grid: {
          containLabel: true,
          top: 5,
          left: 10,
          bottom: 10
        }
      }
      const level3Option = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'value',
          show: false,
          max: 'dataMax'
        },
        yAxis: {
          type: 'category',
          data: data.map(s => s.supplierName).reverse(),
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            },
            show: true
          },
          // 去掉刻度线
          axisLine: { 'show': false },
          axisTick: { 'show': false }
        },
        series: [{
          data: data.map(s => s.level3Num).reverse(),
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            normal: {
              label: {
                show: true, // 开启显示
                position: 'right',
                textStyle: { // 数值样式
                  color: '#5FA4E9',
                  fontSize: 12
                }
              },
              color: '#307CE0'
            }
          }
        }],
        grid: {
          containLabel: true,
          top: 5,
          left: 10,
          bottom: 10
        }
      }
      const level4Option = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'value',
          show: false,
          max: 'dataMax'
        },
        yAxis: {
          type: 'category',
          data: data.map(s => s.supplierName).reverse(),
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            },
            show: true
          },
          // 去掉刻度线
          axisLine: { 'show': false },
          axisTick: { 'show': false }
        },
        series: [{
          data: data.map(s => s.level4Num).reverse(),
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            normal: {
              label: {
                show: true, // 开启显示
                position: 'right',
                textStyle: { // 数值样式
                  color: '#5FA4E9',
                  fontSize: 12
                }
              },
              color: '#307CE0'
            }
          }
        }],
        grid: {
          containLabel: true,
          top: 5,
          left: 10,
          bottom: 10
        }
      }
      level1Chart.setOption(level1Option)
      level2Chart.setOption(level2Option)
      level3Chart.setOption(level3Option)
      level4Chart.setOption(level4Option)
    }

  }
}
